﻿using Audit.Core;
using Audit.EntityFramework.Core.Samples.Infrastructure.CurrentUser;
using Audit.EntityFramework.Core.Samples.Models;
using Microsoft.EntityFrameworkCore;

namespace Audit.EntityFramework.Core.Samples.Data;

public class ApplicationDbContext(
    DbContextOptions<ApplicationDbContext> options,
    ICurrentUser currentUser
) : AuditDbContext(options)
{
    public override void OnScopeCreated(IAuditScope auditScope)
    {
        int? userId = currentUser.IsAuthenticated ? currentUser.UserId : null;
        auditScope.SetCustomField("UserId", userId);
    }
    
    public DbSet<AuditLogEntry> AuditLogs { get; set; }
    public DbSet<TodoItem> TodoItems { get; set; }
    
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
    }
}
